﻿using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command.PregnancyCommand;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
    /// <summary>
    /// 种母妊检
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class PregnancysController : ControllerBase
    {
        private readonly IMediator  _mediator;
        private ILogger<PregnancysController> _logger;
        public PregnancysController(IMediator mediator, ILogger<PregnancysController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 种母妊检列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<PageOutput<PregnancyOutput>>> GetResultsAsync(PregnancyGetCommand command)
        {
            try
            {
                _logger.LogInformation("这是种母妊检列表功能");
                var result = await _mediator.Send(command);
                return Results<PageOutput<PregnancyOutput>>.DataResult(result);
            }
            catch (Exception ex)
            {
                _logger.LogError("种母妊检列表功能异常信息:"+ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 种母添加功能
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> AddPregnancy(List<Sheeppregnancy> sheeppregnancies)
        {
            try
            {
                _logger.LogInformation("这是种母妊检添加功能");
                var command = new PregnancyAddCommand(sheeppregnancies);
                var result = await _mediator.Send(command);
                return Results<int>.DataResult(result);
            }
            catch (Exception ex)
            {
                _logger.LogError("种母妊检添加异常信息:" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 种母删除功能
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> DelePregnancy(PregnancyDeleteCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);
                return Results<int>.DataResult(result);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
}
